package com.yocard.yxyd.ui.presenter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yocard.yxyd.R;
import com.yocard.yxyd.base.CommonSubscriber;
import com.yocard.yxyd.base.Constants;
import com.yocard.yxyd.base.RxPresenter;
import com.yocard.yxyd.http.HttpManager;
import com.yocard.yxyd.http.model.base.Response;
import com.yocard.yxyd.http.model.response.CommonBean;
import com.yocard.yxyd.http.model.response.OrderListBean;
import com.yocard.yxyd.http.model.response.OrderPayResponseBean;
import com.yocard.yxyd.http.model.response.UserInfoBean;
import com.yocard.yxyd.ui.contract.OrderDetailContact;
import com.yocard.yxyd.ui.view.ViewOrderDetail;
import com.yocard.yxyd.utils.CommonUtils;
import com.yocard.yxyd.utils.RxUtil;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

/**
 * Created on 2019/4/8.
 */
public class OrderDetailPresenter extends RxPresenter<OrderDetailContact.View> implements OrderDetailContact.Presenter {

    @Inject
    public OrderDetailPresenter(HttpManager manager) {
        super(manager);
    }

    @Override
    public void commit(String listId) {
        addSubscribe(httpManager.doOrder(listId)
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<CommonBean>(mView) {

                    @Override
                    public void onNext(Response<CommonBean> tResponse) {
                        if (tResponse.getCode() == Constants.STATUS_SUCCESS_4) {
                            mView.onOrderReceived(tResponse);
                            return;
                        }
                        super.onNext(tResponse);
                    }

                    @Override
                    public void onSuccess(CommonBean data) {
                        mView.onCreatedOrder(data.getId());
                    }

                }));
    }

    @Override
    public void fetchUserData() {
        addSubscribe(httpManager.getUswrInfo()
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<UserInfoBean>(mView) {

                    @Override
                    public void onSuccess(UserInfoBean data) {
                        mView.onFetchUser(data);
                    }
                }));
    }

    @Override
    public void fetchData(String id) {
        addSubscribe(httpManager.getOrderDetail(id)
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<OrderListBean.OrderList>(mView) {

                    @Override
                    public void onSuccess(OrderListBean.OrderList data) {
                        try {
                            mView.onFetchData(data);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }));

    }

    @Override
    public void fillView(LinearLayout llContainer, OrderListBean.OrderList orderListBean) {
        llContainer.removeAllViews();
        if (Constants.CAREE_SHANGBAN.equals(orderListBean.getCareer())) {
            ViewOrderDetail view1 = new ViewOrderDetail(mView.getContext(), "银行卡发放工资"
                    , CommonUtils.getFormalInt(orderListBean.getCash_income()) + "元/月", R.color.color_ff812c);
            llContainer.addView(view1);
            ViewOrderDetail view2 = new ViewOrderDetail(mView.getContext(), "工资发放形式"
                    , CommonUtils.getReceivedType(orderListBean.getSalary_received_type()));
            llContainer.addView(view2);
            if (TextUtils.isEmpty(orderListBean.getWork_year())) return;
            ViewOrderDetail view3 = new ViewOrderDetail(mView.getContext(), "当前单位工龄"
                    , CommonUtils.getYear(orderListBean.getWork_year()), R.color.color_ff812c);
            llContainer.addView(view3);
        } else if (Constants.CAREE_ZIYOU.equals(orderListBean.getCareer())) {
            ViewOrderDetail view1 = new ViewOrderDetail(mView.getContext(), "现金收入"
                    , CommonUtils.getFormalInt(orderListBean.getCash_income()) + "元/月", R.color.color_ff812c);
            llContainer.addView(view1);
        } else {
            ViewOrderDetail view1 = new ViewOrderDetail(mView.getContext(), "总经营流水"
                    , CommonUtils.getFormalForQiye(orderListBean.getBusiness_amount()) + "/月", R.color.color_ff812c);
            llContainer.addView(view1);
            if (Constants.CAREE_QIYE.equals(orderListBean.getCareer()) && !TextUtils.isEmpty(orderListBean.getPublic_amount())) {
                ViewOrderDetail view2 = new ViewOrderDetail(mView.getContext(), "对公账户经营收入"
                        , CommonUtils.getFormalForQiye(orderListBean.getPublic_amount()) + "/月", R.color.color_ff812c);
                llContainer.addView(view2);
            }
            if (!TextUtils.isEmpty(orderListBean.getBusiness_year())) {
                ViewOrderDetail view3 = new ViewOrderDetail(mView.getContext(), "经营年限"
                        , CommonUtils.getYear(orderListBean.getBusiness_year()), R.color.color_ff812c);
                llContainer.addView(view3);
            }
            if (!TextUtils.isEmpty(orderListBean.getBusiness_address())) {
                ViewOrderDetail view4 = new ViewOrderDetail(mView.getContext(), "经营注册地"
                        , orderListBean.getBusiness_address());
                llContainer.addView(view4);
            }
            ViewOrderDetail view5 = new ViewOrderDetail(mView.getContext(), "执照信息"
                    , orderListBean.getIs_business_license() == 0 ? "无" : "有", R.color.color_ff812c);
            llContainer.addView(view5);
        }
        ViewOrderDetail view = new ViewOrderDetail(mView.getContext(), "信用状况"
                , CommonUtils.getProfile(orderListBean.getCredit_profile()), R.color.color_ff812c);
        llContainer.addView(view);
    }

    @Override
    public void fillZiliao(TagFlowLayout flowLayout, List<String> ziliao) {
        flowLayout.setAdapter(new TagAdapter<String>(ziliao) {
            @Override
            public View getView(FlowLayout parent, int position, String o) {
                View view = LayoutInflater.from(mView.getContext()).inflate(R.layout.item_ziliao, flowLayout, false);
                TextView tv = view.findViewById(R.id.tv_name);
                ImageView iv = view.findViewById(R.id.iv_ico);
                tv.setText(o);
                if (CommonUtils.getImageRes(o) != 0) {
                    iv.setImageResource(CommonUtils.getImageRes(o));
                } else {
                    iv.setVisibility(View.GONE);
                }
                return view;
            }
        });
    }

    @Override
    public void doPay(String id, int delay) {
        addSubscribe(httpManager.doPay(id)
                .delay(delay, TimeUnit.SECONDS)
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<OrderPayResponseBean>(mView) {

                    @Override
                    public void onNext(Response<OrderPayResponseBean> tResponse) {
                        if (tResponse.getCode() == Constants.STATUS_SUCCESS_4) {
                            mView.onOrderReceived(tResponse);
                            super.onComplete();
                            return;
                        }
                        super.onNext(tResponse);
                    }

                    @Override
                    public void onSuccess(OrderPayResponseBean data) {
                        mView.onPaySuccess(data);
                    }
                }));
    }

}
